/**
 * Copyright (c) 2013-Now http://jeesite.com All rights reserved.
 */
package com.jeesite.modules.workprocedure.web;

import com.jeesite.common.config.Global;
import com.jeesite.common.entity.Page;
import com.jeesite.common.web.BaseController;
import com.jeesite.model.dto.ParamPageDTO;
import com.jeesite.model.dto.ResponseData;
import com.jeesite.model.dto.ResponseUtil;
import com.jeesite.modules.workprocedure.entity.WorkProcedure;
import com.jeesite.modules.workprocedure.service.impl.WorkProcedureService;
import io.swagger.annotations.ApiOperation;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

/**
 * 工序表Controller
 *
 * @author gzm
 * @version 2019-06-15
 */
@Controller
@RequestMapping(value = "/workProcedure")
public class WorkProcedureController extends BaseController {

    @Autowired
    private WorkProcedureService workProcedureService;

    /**
     * 获取数据
     */
    @ModelAttribute
    public WorkProcedure get(String procedureId, boolean isNewRecord) {
        return workProcedureService.get(procedureId, isNewRecord);
    }

    /**
     * 查询列表
     */
//    @RequiresPermissions("workprocedure:workProcedure:view")
    @RequestMapping(value = {"list", ""})
    public String list(WorkProcedure workProcedure, Model model) {
        logger.info("获取工序组信息");
        model.addAttribute("workProcedure", workProcedure);
        return "modules/workprocedure/workProcedureList";
    }

    /**
     * 查询列表数据
     */
//    @RequiresPermissions("workprocedure:workProcedure:view")
    @RequestMapping(value = "listData")
    @ResponseBody
    public Page<WorkProcedure> listData(WorkProcedure workProcedure, HttpServletRequest request, HttpServletResponse response) {
        workProcedure.setPage(new Page<>(request, response));
        Page<WorkProcedure> page = workProcedureService.findPage(workProcedure);
        return page;
    }



    /**
     * 查看编辑表单
     */
//    @RequiresPermissions("workprocedure:workProcedure:view")
    @RequestMapping(value = "form")
    public String form(WorkProcedure workProcedure, Model model) {
        model.addAttribute("workProcedure", workProcedure);
        return "modules/workprocedure/workProcedureForm";
    }

    /**
     * 保存工序表
     */
//    @RequiresPermissions("workprocedure:workProcedure:edit")
//    @PostMapping(value = "/saveProcedure")
//    @ResponseBody
//    public ResponseData save(@Validated WorkProcedure workProcedure) {
//        workProcedureService.save(workProcedure);
//        return ResponseUtil.getSucceed();
//    }

    /**
     * 删除工序表
     */
//    @RequiresPermissions("workprocedure:workProcedure:edit")
//    @RequestMapping(value = "delete")
//    @ResponseBody
//    public String delete(WorkProcedure workProcedure) {
//        workProcedureService.delete(workProcedure);
//        return renderResult(Global.TRUE, text("删除工序表成功！"));
//    }

    @ApiOperation("分页获取工序表")
    @PostMapping("/listProcedures")
    @ResponseBody
    public ResponseData listProcedures(@RequestBody ParamPageDTO page) {
        if (null == page || page.isNotValidPage()) {
            return ResponseUtil.getFailed("page is not valid");
        }
        WorkProcedure workProcedure = new WorkProcedure();
        workProcedure.setPage(new Page<>(page.getPageNo(), page.getPageSize()));
        return ResponseUtil.getSucceed(workProcedureService.findPage(workProcedure));
    }

    @ApiOperation("删除工序")
    @PostMapping("/deleteProcedure")
    @ResponseBody
    public ResponseData deleteProcedure(@RequestParam String proId) {
        if (StringUtils.isEmpty(proId)) {
            return ResponseUtil.getFailed("proId is null");
        }
        workProcedureService.deleteById(proId);
        return ResponseUtil.getSucceed();
    }

    @ApiOperation("新增工序")
    @PostMapping("/saveProcedure")
    @ResponseBody
    public ResponseData saveProcedure(@RequestBody WorkProcedure procedure) {
        if (null == procedure) {
            return ResponseUtil.getFailed("proId is null");
        }
        workProcedureService.save(procedure);
        return ResponseUtil.getSucceed();
    }

    @ApiOperation("修改工序")
    @PostMapping("/updateProcedure")
    @ResponseBody
    public ResponseData updateProcedure(@RequestBody WorkProcedure procedure) {
        if (null == procedure || StringUtils.isEmpty(procedure.getProcedureId())) {
            return ResponseUtil.getFailed("procedure is null");
        }
        workProcedureService.save(procedure);
        return ResponseUtil.getSucceed();
    }

}